package com.itqian.seller.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itqian.seller.dto.BarDTO;
import com.itqian.seller.entity.FoodCategory;
import com.itqian.seller.entity.FoodInfo;
import com.itqian.seller.entity.OrderDetail;
import com.itqian.seller.entity.OrderMaster;
import com.itqian.seller.mapper.*;
import com.itqian.seller.service.OrderMasterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itqian.seller.vo.OrderDetailVO;
import com.itqian.seller.vo.OrderMasterVO;
import com.itqian.seller.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单主表 服务实现类
 * </p>
 *
 * @author shishuqian
 * @since 2022-03-21
 */
@Service
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster> implements OrderMasterService {

    @Autowired
    private OrderMasterMapper orderMasterMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private StoreInfoMapper storeInfoMapper;

    @Autowired
    private FoodCategoryMapper foodCategoryMapper;

    @Autowired
    private FoodInfoMapper foodInfoMapper;

    @Override
    public Map<String, Object> list(Integer storeId, Integer page, Integer size) {
        Page<OrderMaster> orderMasterPage = new Page<>(page,size);
        /**
         *  查询店铺订单条件：已完成支付(pat_status = 1),卖家未删除(seller_deleted = false)
         *  按create_time降序查询
         */
        QueryWrapper<OrderMaster> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_status",1)
                .orderByDesc("create_time");
        if (storeId != null){
            wrapper.eq("store_id",storeId)
                    .eq("seller_deleted",false);
        }else {
            wrapper.eq("admin_deleted",false);
        }

        Page<OrderMaster> orderPage = orderMasterMapper.selectPage(orderMasterPage, wrapper);
        //处理数据格式返回
        return this.orderMasterToVO(orderPage,storeId == null);
    }

    @Override
    public Map<String, Object> like(Integer storeId, Integer page, Integer size, String type, String keyword) {
        Page<OrderMaster> orderMasterPage = new Page<>(page, size);
        QueryWrapper<OrderMaster> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_status",1)
                .orderByDesc("create_time")
                .like(type,keyword);
        if (storeId != null){
            wrapper.eq("store_id",storeId)
                    .eq("seller_deleted",false);
        }else {
            wrapper.eq("admin_deleted",false);
        }
        Page<OrderMaster> masterPage = orderMasterMapper.selectPage(orderMasterPage, wrapper);
        return orderMasterToVO(masterPage,storeId == null);
    }

    @Override
    public boolean delete(Integer orderId) {
        return orderMasterMapper.delete(orderId);
    }

    @Override
    public boolean adminDelete(Integer orderId) {
        return orderMasterMapper.adminDelete(orderId);
    }

    @Override
    public OrderVO detail(Integer orderId) {
        //查询order_master表获取主信息
        OrderMaster orderMaster = orderMasterMapper.selectById(orderId);
        //根据order_id查询order_detail表中的数据
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapper);
        //处理数据格式返回
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(orderMaster,orderVO);

        //查询订单的店铺名
        String storeName = storeInfoMapper.getStoreNameById(orderMaster.getStoreId());
        orderVO.setStoreName(storeName);

        List<OrderDetailVO> list = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetails) {
            OrderDetailVO orderDetailVO = new OrderDetailVO();
            BeanUtils.copyProperties(orderDetail,orderDetailVO);
            list.add(orderDetailVO);
        }
        orderVO.setOrderDetailVOList(list);
        return orderVO;
    }

    //这个方法改order_id类型后没影响
    private Map<String, Object> orderMasterToVO(Page<OrderMaster> masterPage,boolean flag){
        List<OrderMaster> records = masterPage.getRecords();
        List<OrderMasterVO> list = new ArrayList<>();
        for (OrderMaster record : records) {
            OrderMasterVO orderMasterVO = new OrderMasterVO();
            BeanUtils.copyProperties(record, orderMasterVO);
            //管理员查看需要获取店铺名
            if (flag){
                orderMasterVO.setStoreName(this.storeInfoMapper.getStoreNameById(record.getStoreId()));
            }
            list.add(orderMasterVO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("content",list);
        map.put("size",masterPage.getSize());
        map.put("total",masterPage.getTotal());
        return map;
    }


    @Override
    public List<BarDTO> categoryBar(Integer storeId) {
        QueryWrapper<FoodCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",storeId);
        //1.查询出该店铺的所有菜品分类
        List<FoodCategory> foodCategoryList = foodCategoryMapper.selectList(wrapper);

        List<BarDTO> list = new ArrayList<>();

        //2.统计每个分类中，所有菜品的总销量
        for (FoodCategory foodCategory : foodCategoryList) {
            QueryWrapper<FoodInfo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("category_type", foodCategory.getCategoryId());
            List<FoodInfo> foodInfoList = foodInfoMapper.selectList(wrapper1);

            int sum = 0;
            for (FoodInfo foodInfo : foodInfoList) {
                sum += foodInfo.getSellQuantity();
            }
            BarDTO barDTO = new BarDTO(foodCategory.getCategoryName(),sum);
            list.add(barDTO);
        }

        return list;
    }


}
